//
//  YEventInforMTTableViewController.m
//  shijianpinggu
//
//  Created by apple on 2017/1/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YEventInforMTTableViewController.h"
#import "YEventInforMTTableViewCell.h"
@interface YEventInforMTTableViewController ()
@property(nonatomic,strong)NSArray *arrayName;
@property(nonatomic,strong)NSDictionary *dicInfo;
@property(nonatomic,strong)NSMutableArray *cellHeighAr;

@property(nonatomic,assign)NSInteger cellHeigh;

@end

@implementation YEventInforMTTableViewController
-(NSMutableArray *)cellHeighAr{
    if (!_cellHeighAr) {
        self.cellHeighAr = [NSMutableArray new];
    }
    return _cellHeighAr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"检测详情";
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    //去掉返回按钮上的文字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YEventInforMTTableViewCell" bundle:nil] forCellReuseIdentifier:@"eventIfCell"];
    self.dicInfo = [NSDictionary new];
    self.cellHeigh = 0;
    
    [self loadDataFormUrl];
    [self eventType];

    
   
}
-(void)eventType{

    if ([self.type_v isEqualToString:@"black_links"]) {//    暗链
        self.arrayName = [[NSArray alloc] initWithObjects:@"存在暗链的URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"暗链词库列表:",nil];
    }else if([self.type_v isEqualToString:@"malscan"]){//挂马
        self.arrayName = [[NSArray alloc] initWithObjects:@"存在挂马的URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"恶意源URL:",@"规则ID:",@"特征类型:",@"云端恶意源URL:",@"云端恶意嵌入方式:", nil];
    }else if([self.type_v isEqualToString:@"siteinfo"]){//站点信息
        self.arrayName = [[NSArray alloc] initWithObjects:@"评估平台:",@"风险类型:",@"发现时间:",@"评估结果:",@"当前状态:",@"级        别:",@"IP:",@"主机名/域:",@"ICP备案信息:",@"alexa排名:",@"地理位置:", @"域名到期时间:",@"被探测到的操作系统:",@"被探测到服务:",@"PageRank:",nil];
    }else if([self.type_v isEqualToString:@"availability"]){//可用性
        self.arrayName = [[NSArray alloc] initWithObjects:@"结果URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"Ping响应时间:",@"查询时间:",@"是否劫持的值:",@"响应状态:",@"响应时间:", @"DNS返回的域名IP列表:",nil];
    }else if([self.type_v isEqualToString:@"keyword"]){//关键词
        self.arrayName = [[NSArray alloc] initWithObjects:@"存在关键词的URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"关键词等级:",@"关键词类型:",@"关键词:", nil];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.arrayName.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YEventInforMTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eventIfCell" forIndexPath:indexPath];
    
    cell.name.text = self.arrayName[indexPath.row];
//    cell.name.adjustsFontForContentSizeCategory
//    = YES;
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([self.type_v isEqualToString:@"black_links"]) {//    暗链
//        self.arrayName = [[NSArray alloc] initWithObjects:@"存在暗链的URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"暗链词库列表:",nil];
        
        switch (indexPath.row) {
            case 0:
                cell.informotion.text = self.dicInfo[@"url"];                break;
            case 1:
                cell.informotion.text = self.dicInfo[@"type"];
                [cell.informotion setTextColor:[self nameColorWhit:cell.informotion.text]];
                break;
            case 2:
                cell.informotion.text = self.dicInfo[@"created_at"];
                break;
            case 3:
                cell.informotion.text = @"评估完成";
                break;
            case 4:
                cell.informotion.text = @"未处理";
                [cell.informotion setTextColor:[self nameColorWhit:cell.informotion.text]];
                
                break;
            case 5:
                cell.informotion.text = self.dicInfo[@"level"];
                [cell.informotion setTextColor:[self colorWhit:self.dicInfo[@"level"]]];
                break;
            case 6:
                cell.informotion.text = self.dicInfo[@"value_keywords"];
                
                break;
            
            default:
                break;
        }
    }else if([self.type_v isEqualToString:@"malscan"]){//挂马
//        self.arrayName = [[NSArray alloc] initWithObjects:@"存在挂马的URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"恶意源URL:",@"规则ID:",@"特征类型:",@"云端恶意源URL:",@"云端恶意嵌入方式:", nil];
        NSArray *staticAr = self.dicInfo[@"value_static"];
        NSArray *cloAr = self.dicInfo[@"value_cloud"];

        NSString *staUrl = @"";
        NSString *staId = @"";
        NSString *staType = @"";
        NSString *cloUpr = @"";
        NSString *cloTag = @"";
        
        for (int i = 0; i<staticAr.count; i++) {
            NSString *strsta = self.dicInfo[@"value_static"][i][@"url"];
            NSString *starid = (self.dicInfo[@"value_static"])[i][@"rule_id"];
            NSString *strtype = self.dicInfo[@"value_static"][i][@"type"];
            if (i>0) {
                staUrl = [staUrl stringByAppendingString:@",  "];
                staUrl = [staUrl stringByAppendingString:strsta];
                
                staId = [staId stringByAppendingString:@",  "];
                staId = [staId stringByAppendingString:starid];
                
                staType = [staType stringByAppendingString:@",  "];
                staType = [staType stringByAppendingString:strtype];
                
            }else{
                 staUrl = [staUrl stringByAppendingString:strsta];
                 staId = [staId stringByAppendingString:starid];
                 staType = [staId stringByAppendingString:strtype];
                
            }
        }
        for (int i = 0; i<cloAr.count; i++) {
            
            NSString *strurl = (self.dicInfo[@"value_cloud"])[i][@"url"];
            NSString *strtag = (self.dicInfo[@"value_cloud"])[i][@"tag"];
            
            if (i>0) {
                cloUpr = [cloUpr stringByAppendingString:@",  "];
                cloUpr = [cloUpr stringByAppendingString:strurl];
                
                cloTag = [cloTag stringByAppendingString:@",  "];
                cloTag = [cloTag stringByAppendingString:strtag];
                
            }else{
                cloUpr = [cloUpr stringByAppendingString:strurl];
                cloTag = [cloTag stringByAppendingString:strtag];
                
            }
        }

       
        switch (indexPath.row) {
            case 0:
                cell.informotion.text = self.dicInfo[@"url"];                break;
            case 1:
                cell.informotion.text = self.dicInfo[@"type"];
                break;
            case 2:
                cell.informotion.text = self.dicInfo[@"created_at"];
                break;
            case 3:
                cell.informotion.text = @"评估完成";
                break;
            case 4:
                cell.informotion.text = @"未处理";
                [cell.informotion setTextColor:[self nameColorWhit:cell.informotion.text]];
                break;
            case 5:
                cell.informotion.text = self.dicInfo[@"level"];
                [cell.informotion setTextColor:[self colorWhit:self.dicInfo[@"level"]]];
                break;
            case 6:
                cell.informotion.text = staUrl;
                break;
            case 7:
                cell.informotion.text = staId;
                break;
            case 8:
                cell.informotion.text = staType;
                break;
            case 9:
                cell.informotion.text = cloUpr;
                break;
            case 10:
                cell.informotion.text = cloTag;
                break;
            default:
                break;
        }

    }else if([self.type_v isEqualToString:@"siteinfo"]){//站点信息
//        self.arrayName = [[NSArray alloc] initWithObjects:@"评估平台:",@"风险类型:",@"发现时间:",@"评估结果:",@"当前状态:",@"级        别:",@"IP:",@"主机名/域:",@"ICP备案信息:",@"alexa排名:",@"地理位置:", @"域名到期时间:",@"呗探测到的操作系统:",@"被探测到服务:",@"PageRank:",nil];
        
        switch (indexPath.row) {
            case 0:
                cell.informotion.text = self.dicInfo[@"url"];                break;
            case 1:
                cell.informotion.text = self.dicInfo[@"type"];
                break;
            case 2:
                cell.informotion.text = self.dicInfo[@"created_at"];
                break;
            case 3:
                cell.informotion.text = @"评估完成";
                break;
            case 4:
                cell.informotion.text = @"未处理";
                [cell.informotion setTextColor:[self nameColorWhit:cell.informotion.text]];
                break;
            case 5:
                cell.informotion.text = self.dicInfo[@"level"];
                [cell.informotion setTextColor:[self colorWhit:self.dicInfo[@"level"]]];
                break;
            case 6:
                cell.informotion.text = self.dicInfo[@"value_ip"];
                break;
            case 7:
                cell.informotion.text = self.dicInfo[@"value_host"];
                break;
            case 8:
                cell.informotion.text = self.dicInfo[@"value_icp"];
                break;
            case 9:
                cell.informotion.text = self.dicInfo[@"value_alexa"];
                break;
            case 10:
                if (!self.dicInfo[@"value_location"]) {
                    cell.informotion.text = self.dicInfo[@"value_location"];
                }
                break;
            case 11:
                cell.informotion.text = self.dicInfo[@"value_whois"];
            case 12:
                cell.informotion.text = self.dicInfo[@"value_os"];
            case 13:
                cell.informotion.text = self.dicInfo[@"value_server"];
            case 14:
                cell.informotion.text = self.dicInfo[@"value_pr"];
                
            default:
            break;}
    }else if([self.type_v isEqualToString:@"availability"]){//可用性
//        self.arrayName = [[NSArray alloc] initWithObjects:@"结果URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"Ping响应时间:",@"查询时间:",@"是否劫持的值:",@"响应状态:",@"响应时间:", @"DNS返回的域名IP列表:",nil];
        switch (indexPath.row) {
            case 0:
                cell.informotion.text = self.dicInfo[@"url"];                break;
            case 1:
                cell.informotion.text = self.dicInfo[@"type"];
                break;
            case 2:
                cell.informotion.text = self.dicInfo[@"created_at"];
                break;
            case 3:
                cell.informotion.text = @"评估完成";
                break;
            case 4:
                cell.informotion.text = @"未处理";
                [cell.informotion setTextColor:[self nameColorWhit:cell.informotion.text]];
                break;
            case 5:
                cell.informotion.text = self.dicInfo[@"level"];
                [cell.informotion setTextColor:[self colorWhit:self.dicInfo[@"level"]]];
                break;
            case 6:
                if (!self.dicInfo[@"ping"]) {
                    cell.informotion.text = self.dicInfo[@"ping"];
                }
                
                break;
            case 7:
                cell.informotion.text = [NSString stringWithFormat:@"%@",self.dicInfo[@"dns_time"]];
                break;
            case 8:
                cell.informotion.text = self.dicInfo[@"dns_hijack_hijacked"];
                break;
            case 9:
                cell.informotion.text = self.dicInfo[@"http_get_status"];
                break;
            case 10:
                cell.informotion.text = self.dicInfo[@"http_get_time"];
                break;
            case 11:
                cell.informotion.text = self.dicInfo[@"dns_address"];
                break;

            default:
                break;
        }
    }  else if([self.type_v isEqualToString:@"keyword"]){//关键词
//        self.arrayName = [[NSArray alloc] initWithObjects:@"存在关键词的URL:",@"结果类型:",@"结果创建时间:",@"评估结果:",@"当前状态:",@"级        别:",@"关键词等级:",@"关键词类型:",@"关键词:", nil];
//        
        switch (indexPath.row) {
            case 0:
                cell.informotion.text = self.dicInfo[@"url"];
                break;
            case 1:
                cell.informotion.text = self.dicInfo[@"type"];
                break;
            case 2:
                cell.informotion.text = self.dicInfo[@"created_at"];
                break;
            case 3:
                cell.informotion.text = @"评估完成";
                break;
            case 4:
                cell.informotion.text = @"未处理";
                [cell.informotion setTextColor:[self nameColorWhit:cell.informotion.text]];
                break;
            case 5:
                cell.informotion.text = self.dicInfo[@"level"];
                [cell.informotion setTextColor:[self colorWhit:self.dicInfo[@"level"]]];
                break;
            case 6:
                
                cell.informotion.text = [NSString stringWithFormat:@"%@",(self.dicInfo[@"value"])[@"keywords"][0][@"level"]];
                break;
            case 7:
                cell.informotion.text = [NSString stringWithFormat:@"%@",(self.dicInfo[@"value"])[@"keywords"][0][@"type"]];
                break;
            case 8:
                cell.informotion.text = (self.dicInfo[@"value"])[@"keywords"][0][@"keyword"];
                break;
                
            default:
                break;
        }

    }
//    if (indexPath.row == 4) {
//        [cell.informotion setTextColor:[UIColor redColor]];
//    }
//    if (indexPath.row == 5) {
//        [cell.informotion setTextColor:[UIColor colorWithRed:254/255.0 green:161/255.0 blue:0/255.0 alpha:1]];
//    }
    

    //设置字间距 NSKernAttributeName:@1.5f
    CGFloat width;
    if ([UIScreen mainScreen].bounds.size.width == 414) {
        width = 315.333;
    }else if([UIScreen mainScreen].bounds.size.width == 375){
        width = 276.5;
    }else{
        width = 220;
    }
    
    
    CGRect newSize = [cell.informotion.text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    CGRect newSizeOneLine = [@"得到一行文字的高度" boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    NSInteger cellHeighOneLine = newSizeOneLine.size.height;
    NSInteger cellHeigh = newSize.size.height;
    NSLog(@"%ld",(long)cellHeighOneLine);
    NSLog(@"%ld",(long)cellHeigh);
    
    if (cellHeigh > cellHeighOneLine) {
        self.cellHeigh =  50+ cellHeigh - cellHeighOneLine;
        
    }else{
        self.cellHeigh = 50;
    }
    [self.cellHeighAr addObject:[NSString stringWithFormat:@"%ld",(long)_cellHeigh]];
    
    return cell;
}


-(UIColor *)colorWhit:(NSString *)sender{
    UIColor *color;
    if ([sender isEqualToString:@"安全"]) {
        color = [UIColor colorWithRed:0/255.0 green:110/255.0 blue:171/255.0 alpha:1];
    }else if([sender isEqualToString:@"低危"]){
        color = [UIColor colorWithRed:252/255.0 green:151/255.0 blue:0/255.0 alpha:1];
    }else if([sender isEqualToString:@"中危"]){
        color = [UIColor colorWithRed:255/255.0 green:17/255.0 blue:0/255.0 alpha:1];
    }else if([sender isEqualToString:@"高危"]){
        color = [UIColor colorWithRed:205/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    }

    return  color;
}
-(UIColor *)nameColorWhit:(NSString *)sender{
    UIColor *color;
    if (![sender isEqualToString:@"未处理"]) {
        
        color = [UIColor colorWithRed:31/255.0 green:33/255.0 blue:36/255.0 alpha:1];
    }else{
        color = [UIColor colorWithRed:205/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    }
    
    return  color;
}

-(NSInteger)labelHeighWhith:(NSString *)sender{
    
        CGRect newSize = [sender boundingRectWithSize:CGSizeMake(200, [UIScreen mainScreen].bounds.size.width) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
        NSInteger cellHeigh = newSize.size.height;
//        [self.cellHeighAr addObject:[NSString stringWithFormat:@"%ld",(long)cellHeigh]];
    
    return cellHeigh;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.cellHeigh;
    
}
//请求数据
-(void)loadDataFormUrl{
    
    
    NSDictionary *parameter = @{@"type_v":self.type_v,@"type_id":self.type_id};
    
    //    NSLog(@"%@",dict);
    
    // 初始化Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 不加上这句话，会报“Request failed: unacceptable content-type: text/plain”错误，因为我们要获取text/plain类型数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // post请求
    [manager POST:@"http://wangan.ldynet.cn/index.php/IndexApp/Api_list_detail" parameters:parameter constructingBodyWithBlock:^(id  _Nonnull formData) {
        // 拼接data到请求体，这个block的参数是遵守AFMultipartFormData协议的。
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 请求成功，解析数据
       
//        NSData *jsonData = [responseObject dataUsingEncoding:NSUTF8StringEncoding];
//        
//        NSError *err;
        
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"aaaaaaaa%@", dic);
        
        self.dicInfo = dic;
        
        
        
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 请求失败
        // [self alertWithString:@"请稍后再试"];
    }];
    
    
    //[self loginUrl];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
